#ifndef TASKMANAGER_H
#define TASKMANAGER_H

#include <QObject>
#include <QQueue>
#include <QPair>
#include <QMutex>
#include <QWaitCondition>
#include "backupmanager.h"
#include "syncmanager.h"

class TaskManager : public QObject {
    Q_OBJECT

public:
    explicit TaskManager(BackupManager *backupManager, SyncManager *syncManager, QObject *parent = nullptr);

    // 添加备份任务到队列
    void addBackupTask(const QString &source, const QString &target, bool incremental);

    // 添加同步任务到队列
    void addSyncTask(const QString &source, const QString &target, bool resolveConflicts);
    void startTasks();
    // 获取当前的备份任务计数器值
    int getBackupCounter() const { return m_backupCounter; }
    // 获取当前的同步任务计数器值
    int getSyncCounter() const { return m_syncCounter; }
    // 执行单个任务
    void executeTask(int taskId);


signals:
    // 任务开始信号
    void taskStarted(const QString &description);
    // 任务完成信号
    void taskCompleted(const QString &description);

private slots:
    void onBackupFinished();
    void onSyncFinished();

private:
    void processTasks();
    void startNextTask();


    BackupManager *m_backupManager;
    SyncManager *m_syncManager;
    QQueue<QPair<QString, QString>> m_taskQueue;  // 任务队列
    QMutex m_mutex;  // 互斥锁
    QWaitCondition m_waitCondition;  // 等待条件


    enum TaskType { Backup, Sync };
    struct Task {
        int id;
        TaskType type;
        QString source;
        QString target;
        bool incremental;
        bool resolveConflicts;
    };
    QList<Task> m_tasks;  // 任务列表
    int m_backupCounter;  // 备份任务的ID计数器
    int m_syncCounter;    // 同步任务的ID计数器
    void removeCompletedTask(TaskType type);

};

#endif // TASKMANAGER_H
